package com.ruoyi.reportInfo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportInfo.domain.bo.IndustrialLogisticsCompanyOperationBo;
import com.ruoyi.reportInfo.domain.vo.IndustrialLogisticsCompanyOperationVo;
import com.ruoyi.reportInfo.domain.IndustrialLogisticsCompanyOperation;
import com.ruoyi.reportInfo.mapper.IndustrialLogisticsCompanyOperationMapper;
import com.ruoyi.reportInfo.service.IIndustrialLogisticsCompanyOperationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 物流企业运营信息Service业务层处理
 *
 * @author yangzhen
 * @date 2025-11-27
 */
@RequiredArgsConstructor
@Service
public class IndustrialLogisticsCompanyOperationServiceImpl implements IIndustrialLogisticsCompanyOperationService {

    private final IndustrialLogisticsCompanyOperationMapper baseMapper;

    /**
     * 查询物流企业运营信息
     */
    @Override
    public IndustrialLogisticsCompanyOperationVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询物流企业运营信息列表
     */
    @Override
    public TableDataInfo<IndustrialLogisticsCompanyOperationVo> queryPageList(IndustrialLogisticsCompanyOperationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialLogisticsCompanyOperation> lqw = buildQueryWrapper(bo);
        Page<IndustrialLogisticsCompanyOperationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询物流企业运营信息列表
     */
    @Override
    public List<IndustrialLogisticsCompanyOperationVo> queryList(IndustrialLogisticsCompanyOperationBo bo) {
        LambdaQueryWrapper<IndustrialLogisticsCompanyOperation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IndustrialLogisticsCompanyOperation> buildQueryWrapper(IndustrialLogisticsCompanyOperationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IndustrialLogisticsCompanyOperation> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyId()), IndustrialLogisticsCompanyOperation::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getReportDate() != null, IndustrialLogisticsCompanyOperation::getReportDate, bo.getReportDate());
        lqw.eq(bo.getOperationWeight() != null, IndustrialLogisticsCompanyOperation::getOperationWeight, bo.getOperationWeight());
        lqw.eq(bo.getOperationCars() != null, IndustrialLogisticsCompanyOperation::getOperationCars, bo.getOperationCars());
        lqw.eq(bo.getReportCollectDate() != null, IndustrialLogisticsCompanyOperation::getReportCollectDate, bo.getReportCollectDate());
        lqw.eq(StringUtils.isNotBlank(bo.getReportCycle()), IndustrialLogisticsCompanyOperation::getReportCycle, bo.getReportCycle());
        return lqw;
    }

    /**
     * 新增物流企业运营信息
     */
    @Override
    public Boolean insertByBo(IndustrialLogisticsCompanyOperationBo bo) {
        IndustrialLogisticsCompanyOperation add = BeanUtil.toBean(bo, IndustrialLogisticsCompanyOperation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改物流企业运营信息
     */
    @Override
    public Boolean updateByBo(IndustrialLogisticsCompanyOperationBo bo) {
        IndustrialLogisticsCompanyOperation update = BeanUtil.toBean(bo, IndustrialLogisticsCompanyOperation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IndustrialLogisticsCompanyOperation entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除物流企业运营信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
